package com.qire.manhua.controller;

import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Navigation;
import com.qire.manhua.BR;
import com.qire.manhua.activity.HomeActivity;
import com.qire.manhua.databinding.FragmentRecommendHotBinding;
import com.qire.manhua.fragment.RecommendHotFragment;
import com.qire.manhua.model.entity.HomeModuleEntity.HomeModuleRankEntity.HomeModuleRankItemEntity;
import com.qire.manhua.model.enumeration.PageTabIndex;
import com.qire.manhua.model.viewModel.impl.RecommendHotViewModel;
import com.qire.manhua.model.viewModel.impl.RecommendHotViewModel.HotSlideZoneViewModel.HotBannerItemViewModel;
import com.qire.manhua.model.viewModel.impl.RecommendModuleRankViewModel.ModuleRankTabViewModel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;

public class RecommendHotController extends RecommendModuleController<RecommendHotFragment> implements RecommendModuleRecordController, RecommendModuleRankController {

    private RecommendHotViewModel viewModel;

    public RecommendHotController(@NonNull RecommendHotFragment owner) {
        super(owner);
    }

    @Override
    protected void bindView() {
        FragmentRecommendHotBinding viewDataBinder = owner.viewDataBinder();
        if(viewDataBinder != null) {
            viewDataBinder.setRecommendHotController(this);
        }
    }

    @Override
    public void bindVariable(ViewDataBinding viewBind) {
        super.bindVariable(viewBind);
        viewBind.setVariable(BR.recommendHotController, this);
        viewBind.setVariable(BR.recommendModuleRecordController, this);
        viewBind.setVariable(BR.recommendModuleRankController, this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(RecommendHotViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/
    public void refreshLayoutOnRefresh(RefreshLayout refreshLayout) {
        viewModel.loadHotModuleData();
    }

    /**
     * 重载刷新按钮事件
     */
    public void reloadBtnOnClick(View view) {
        viewModel.loadHotModuleData();
    }

    /**
     * 幻灯横幅广告点击事件
     */
    public void slideBannerItemOnClick(View view) {
        HotBannerItemViewModel bannerItemViewModel = ObjectUtil.as(view.getTag(), HotBannerItemViewModel.class);
        if (ObjectUtil.nonNull(bannerItemViewModel)) {
            bannerItemViewModel.doAction();
        }
    }

    /**
     * 排行榜导航按钮
     */
    public void rankNavBtnOnClick(View view) {
        int tabIndex = ObjectUtil.as(view.getTag(), Integer.class, PageTabIndex.TAB_INDEX_INVALID);
        if(tabIndex != PageTabIndex.TAB_INDEX_INVALID) {
            AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicRankListOfHotPage)
                    .withData("pageTabIndex", tabIndex).navigation();
        } else {
            AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicRankListOfVipPage).navigation();
        }
    }

    @Override
    public void moduleRecordMoreBtnOnClick(View view) {
        // 最近阅读：阅读记录
        HomeActivity homeActivity = ObjectUtil.as(owner.getActivity(), HomeActivity.class);
        if(ObjectUtil.nonNull(homeActivity)) {
            homeActivity.gotoTabIndex(PageTabIndex.TAB_INDEX_HOME_BOOKSHELF, PageTabIndex.TAB_INDEX_BOOKSHELF_HISTORY);
        }
    }

    @Override
    public void moduleRankTabBtnOnClick(View view) {
        // 排行榜：选项卡标签
        ModuleRankTabViewModel rankViewModel = ObjectUtil.as(view.getTag(), ModuleRankTabViewModel.class);
        if(ObjectUtil.nonNull(rankViewModel)) {
            rankViewModel.gotoTabPage();
        }
    }

    @Override
    public void moduleRankMoreBtnOnClick(View view) {
        // 排行榜：更多
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicRankListOfHotPage)
                .withData("pageTabIndex", PageTabIndex.TAB_INDEX_COMIC_RANK_HOT_END)
                .navigation();
    }

    @Override
    public void moduleRankItemBtnOnClick(View view) {
        // 排行榜：子选项
        HomeModuleRankItemEntity itemEntity = ObjectUtil.as(view.getTag(), HomeModuleRankItemEntity.class);
        if(ObjectUtil.nonNull(itemEntity)) {
            AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicQuickReadPage)
                    .withData("comicBookEntity", itemEntity.toComicBookEntity())
                    .navigation();
        }
    }

}
